package br.edu.universo.si.projeto2.infojob.client.apresentacao;

import br.edu.universo.si.projeto2.infojob.client.bean.BeanEstado;
import br.edu.universo.si.projeto2.infojob.client.bean.BeanEstadoCivil;
import br.edu.universo.si.projeto2.infojob.client.bean.BeanProfissionalAssinante;
import br.edu.universo.si.projeto2.infojob.client.componentes.CEPBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.CPFBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.DateBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.EMailBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.NumberBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.comboboxes.EstadosCivisListBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.comboboxes.EstadosListBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes.ErrorDialogBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes.InformationDialogBox;
import br.edu.universo.si.projeto2.infojob.client.componentes.dialogboxes.SuccessDialogBox;
import br.edu.universo.si.projeto2.infojob.client.util.Util;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class ACadastroProfissional extends DockPanel
{
	private FlexTable flexTable;

	private HorizontalPanel rodape;

	private Button cadastrarBtn;
	private Button cancelarBtn;

	private TextBox nomeTxt;
	private CPFBox cpfTxt;
	private NumberBox rgTxt;
	private NumberBox telefoneResidencialTxt;
	private NumberBox celularTxt;
	private EMailBox emailTxt;
	private DateBox dataNascimentoTxt;
	private EstadosCivisListBox estadoCivilListbox;
	private TextBox enderecoTxt;
	private NumberBox numeroTxt;
	private TextBox bairroTxt;
	private TextBox complementoTxt;
	private TextBox cidadeTxt;
	private EstadosListBox estadosListBox;
	private CEPBox cepTxt;
	private TextBox contaTxt;
	private PasswordTextBox senhaTxt;

	public ACadastroProfissional()
	{
		inicializarComponentes();
		setarTamanhoCampos();

		flexTable.setCellSpacing(5);

		rodape.setSpacing(3);

		flexTable.setWidget(0, 0, new HTML("Cadastro de Profissional"));
		flexTable.getFlexCellFormatter().setColSpan(0, 0, 2);
		flexTable.getFlexCellFormatter().setStyleName(0, 0, "tituloFormCadastro");

		flexTable.setWidget(1, 0, new HTML("<b>* Nome:</b> "));
		flexTable.setWidget(1, 1, nomeTxt);
		flexTable.getFlexCellFormatter().setAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(2, 0, new HTML("<b>* CPF:</b> "));
		flexTable.setWidget(2, 1, cpfTxt);
		flexTable.getFlexCellFormatter().setAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(3, 0, new HTML("RG: "));
		flexTable.setWidget(3, 1, rgTxt);
		flexTable.getFlexCellFormatter().setAlignment(3, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(4, 0, new HTML("Data de nascimento: "));
		flexTable.setWidget(4, 1, dataNascimentoTxt);
		flexTable.getFlexCellFormatter().setAlignment(4, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(5, 0, new HTML("Estado Civil: "));
		flexTable.setWidget(5, 1, estadoCivilListbox);
		flexTable.getFlexCellFormatter().setAlignment(5, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(6, 0, new HTML("Telefone Residencial: "));
		flexTable.setWidget(6, 1, telefoneResidencialTxt);
		flexTable.getFlexCellFormatter().setAlignment(6, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(7, 0, new HTML("Celular: "));
		flexTable.setWidget(7, 1, celularTxt);
		flexTable.getFlexCellFormatter().setAlignment(7, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);
		
		flexTable.setWidget(8, 0, new HTML("E-Mail: "));
		flexTable.setWidget(8, 1, emailTxt);
		flexTable.getFlexCellFormatter().setAlignment(8, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(9, 0, new HTML("Endereço: "));
		flexTable.setWidget(9, 1, enderecoTxt);
		flexTable.getFlexCellFormatter().setAlignment(9, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(10, 0, new HTML("CEP: "));
		flexTable.setWidget(10, 1, cepTxt);
		flexTable.getFlexCellFormatter().setAlignment(10, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(11, 0, new HTML("Número: "));
		flexTable.setWidget(11, 1, numeroTxt);
		flexTable.getFlexCellFormatter().setAlignment(11, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(12, 0, new HTML("Bairro"));
		flexTable.setWidget(12, 1, bairroTxt);
		flexTable.getFlexCellFormatter().setAlignment(12, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(13, 0, new HTML("Complemento: "));
		flexTable.setWidget(13, 1, complementoTxt);
		flexTable.getFlexCellFormatter().setAlignment(13, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(14, 0, new HTML("Cidade: "));
		flexTable.setWidget(14, 1, cidadeTxt);
		flexTable.getFlexCellFormatter().setAlignment(14, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(15, 0, new HTML("Escolha o estado: "));
		flexTable.setWidget(15, 1, estadosListBox);
		flexTable.getFlexCellFormatter().setAlignment(15, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(16, 0, new HTML("<b>* Conta:</b> "));
		flexTable.setWidget(16, 1, contaTxt);
		flexTable.getFlexCellFormatter().setAlignment(16, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		flexTable.setWidget(17, 0, new HTML("<b>* Senha:</b> "));
		flexTable.setWidget(17, 1, senhaTxt);
		flexTable.getFlexCellFormatter().setAlignment(17, 0, HasHorizontalAlignment.ALIGN_RIGHT, HasVerticalAlignment.ALIGN_MIDDLE);

		rodape.add(cadastrarBtn);
		rodape.add(cancelarBtn);

		flexTable.setWidget(18, 0, rodape);
		flexTable.getFlexCellFormatter().setColSpan(18, 0, 2);
		flexTable.getFlexCellFormatter().setAlignment(18, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);

		this.add(flexTable, DockPanel.CENTER);

		// CADASTRAR PROFISSIONAL
		cadastrarBtn.addClickListener(
			new ClickListener()
			{
				public void onClick(Widget sender)
				{
					BeanProfissionalAssinante bProfissionalAssinante = new BeanProfissionalAssinante();

					bProfissionalAssinante.setCpf(cpfTxt.getText());
					bProfissionalAssinante.setRg(rgTxt.getText());
					bProfissionalAssinante.setNome(nomeTxt.getText());
					bProfissionalAssinante.setTelefoneResidencial(telefoneResidencialTxt.getText());
					bProfissionalAssinante.setCelular(celularTxt.getText());
					bProfissionalAssinante.setEmail(emailTxt.getText());
					bProfissionalAssinante.setDataNascimento(dataNascimentoTxt.getText());

					BeanEstadoCivil bEstadoCivil = new BeanEstadoCivil();
					bEstadoCivil.setId(estadoCivilListbox.getValue(estadoCivilListbox.getSelectedIndex()));
					bEstadoCivil.setEstadoCivil(estadoCivilListbox.getItemText(estadoCivilListbox.getSelectedIndex()));

					bProfissionalAssinante.setBEstadoCivil(bEstadoCivil);
					bProfissionalAssinante.setEndereco(enderecoTxt.getText());
					bProfissionalAssinante.setNumero(numeroTxt.getText());
					bProfissionalAssinante.setBairro(bairroTxt.getText());
					bProfissionalAssinante.setComplemento(complementoTxt.getText());
					bProfissionalAssinante.setCidade(cidadeTxt.getText());

					BeanEstado bEstado = new BeanEstado();
					bEstado.setId(estadosListBox.getValue(estadosListBox.getSelectedIndex()));
					bEstado.setEstado(estadosListBox.getItemText(estadosListBox.getSelectedIndex()));

					bProfissionalAssinante.setBEstado(bEstado);
					bProfissionalAssinante.setCep(cepTxt.getText());
					bProfissionalAssinante.setConta(contaTxt.getText());
					bProfissionalAssinante.setSenha(senhaTxt.getText());

					AsyncCallback<Object> callback = new AsyncCallback<Object>()
					{
						public void onSuccess(Object result)
						{
							SuccessDialogBox successDialobBox = new SuccessDialogBox("Profissional cadastrado com sucesso.", 
									new SuccessDialogBox.SuccessCallback()
									{
										public void execute()
										{
											RootPanel.get().remove(ACadastroProfissional.this);
											RootPanel.get().add(new APrincipal());
										}
									}
							);
							successDialobBox.center();
							successDialobBox.show();
						};

						public void onFailure(Throwable caught)
						{
							ErrorDialogBox errorDialogBox = new ErrorDialogBox(
									"Erro ao cadastrar Profissional: " + caught.getMessage());

							errorDialogBox.center();
							errorDialogBox.show();
						};
					};

					if (verificarCampos())
					{
						Util.getService().cadastrarProfissionalAssinante(bProfissionalAssinante, callback);
					}
				}
			}
		);
		
		// CANCELAR CADASTRO
		cancelarBtn.addClickListener(
			new ClickListener()
			{
				public void onClick(Widget sender)
				{
					RootPanel.get().remove(ACadastroProfissional.this);
					RootPanel.get().add(new APrincipal());
				}
			}
		);

	}

	private void inicializarComponentes()
	{
		flexTable = new FlexTable();

		rodape = new HorizontalPanel();

		cadastrarBtn = new Button("Cadastrar");
		cancelarBtn = new Button("Cancelar");

		nomeTxt = new TextBox();
		cpfTxt = new CPFBox();
		rgTxt = new NumberBox(120);
		telefoneResidencialTxt = new NumberBox(100);
		celularTxt = new NumberBox(100);
		emailTxt = new EMailBox();
		dataNascimentoTxt = new DateBox();
		estadoCivilListbox = new EstadosCivisListBox();
		enderecoTxt = new TextBox();
		numeroTxt = new NumberBox(50);
		bairroTxt = new TextBox();
		complementoTxt = new TextBox();
		cidadeTxt = new TextBox();
		estadosListBox = new EstadosListBox();
		cepTxt = new CEPBox();
		contaTxt = new TextBox();
		senhaTxt = new PasswordTextBox();
	}
	
	// define tamanho para cada campo da tela
	private void setarTamanhoCampos()
	{
		nomeTxt.setWidth("300px");
		nomeTxt.setMaxLength(40);
		rgTxt.setMaxLength(10);
		telefoneResidencialTxt.setMaxLength(16);
		celularTxt.setMaxLength(16);
		enderecoTxt.setWidth("250px");
		enderecoTxt.setMaxLength(40);
		numeroTxt.setMaxLength(4);
		bairroTxt.setWidth("100px");
		bairroTxt.setMaxLength(40);
		complementoTxt.setWidth("200px");
		complementoTxt.setMaxLength(100);
		cidadeTxt.setWidth("100px");
		cidadeTxt.setMaxLength(40);
		contaTxt.setWidth("120px");
		contaTxt.setMaxLength(20);
		senhaTxt.setWidth("120px");
		senhaTxt.setMaxLength(20);
	}
	
	// verifica se o preenchimento dos campos estao corretos...
	private boolean verificarCampos()
	{
		if (nomeTxt.getText().equalsIgnoreCase(""))
		{
			InformationDialogBox infoDialogBox = new InformationDialogBox("Preencha o campo 'nome'.");
			infoDialogBox.center();
			infoDialogBox.show();

			return false;
		}

		if (cpfTxt.isVazio())
		{
			InformationDialogBox infoDialogBox = new InformationDialogBox("Preencha o campo 'CPF'.");
			infoDialogBox.center();
			infoDialogBox.show();
			
			return false;
		}

		if (!cpfTxt.isCPFValido())
		{
			InformationDialogBox infoDialogBox = new InformationDialogBox("CPF inválido.");
			infoDialogBox.center();
			infoDialogBox.show();
			
			return false;
		}

		if (contaTxt.getText().equalsIgnoreCase(""))
		{
			InformationDialogBox infoDialogBox = new InformationDialogBox("Campo 'conta' não preenchido.");
			infoDialogBox.center();
			infoDialogBox.show();

			return false;			
		}

		if (senhaTxt.getText().equalsIgnoreCase(""))
		{
			InformationDialogBox infoDialogBox = new InformationDialogBox("Campo 'senha' não preenchido.");
			infoDialogBox.center();
			infoDialogBox.show();

			return false;
		}
		
		if (senhaTxt.getText().length() < 6)
		{
			InformationDialogBox infoDialogBox = new InformationDialogBox("A senha deve ter no mínimo 6 caracteres.");
			infoDialogBox.center();
			infoDialogBox.show();

			senhaTxt.setText("");

			return false;			
		}
		
		if (!emailTxt.isVazio() && !emailTxt.isEMailValido())
		{
			InformationDialogBox infoDialogBox = new InformationDialogBox("E-Mail inválido.");
			infoDialogBox.center();
			infoDialogBox.show();

			return false;			
		}
		
		return true;
	}
}